package com.example.pei.ithinklearnt.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.anenn.flowlikeviewlib.FlowLikeView;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.bean.AlumniCircle;
import com.example.pei.ithinklearnt.view.activity.FrescoLoaderPicturesActivity;
import com.example.pei.ithinklearnt.view.customview.JustifyTextView;
import com.jaeger.ninegridimageview.NineGridImageView;
import com.jaeger.ninegridimageview.NineGridImageViewAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static android.view.MotionEvent.ACTION_CANCEL;
import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;
import static android.view.MotionEvent.ACTION_UP;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;



public class PersonalDynamicAdapter extends RecyclerView.Adapter<PersonalDynamicAdapter.ViewHolder> implements View.OnClickListener,View.OnLongClickListener{

    public static final int TYPE_HEADER = 0;  //说明是带有Header的
    public static final int TYPE_FOOTER = 1;  //说明是带有Footer的
    public static final int TYPE_NORMAL = 2;  //说明是不带有header和footer的

    private Context mContext;
    private List<AlumniCircle> mList;
    private LayoutInflater inflater;



    //
    private itemListener mItemListener;
    public  interface itemListener{
        void onThreePointClick(View v,int position);
        void onHeartClick(View v,int position);
        void onItemClick(View view , int position);
        void onItemLongClick(View view , int position);
    }

    public PersonalDynamicAdapter(Context context ,itemListener mItemListener) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mList = new ArrayList<>();
        this.mItemListener=mItemListener;

    }
    public List<AlumniCircle> getmList(){return mList;}
    public void clearDate(){
        mList.clear();
    }
    public void setList(List<AlumniCircle> list) {
        mList = list;
    }


    //添加到第一个位置
    public void addDatetoFoot(AlumniCircle newdata) {
        mList.add(newdata);
    }
    //HeaderView和FooterView的get和set函数



    @Override
    public void onClick(View view) {
        mItemListener.onItemClick(view,(int)view.getTag());
    }

    @Override
    public boolean onLongClick(View view) {
        mItemListener.onItemLongClick(view,(int)view.getTag());
        return false;
    }

    //创建View，如果是HeaderView或者是FooterView，直接在Holder中返回
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View convertView = inflater.inflate(R.layout.item_personal_dynamic, parent, false);
        convertView.setOnClickListener(this);      //设置监听
        convertView.setOnLongClickListener(this);  //设置长按监听
        return new ViewHolder(convertView);

    }
    @Override
    public void onBindViewHolder(final ViewHolder holder, @SuppressLint("RecyclerView") final int position) {

        if(getItemViewType(position) == TYPE_NORMAL){


            {
                //设置九宫格图片
                holder.mNglContent.setImagesData(mList.get(position).getphotos(),NineGridImageView.STYLE_GRID);
                //设置发送时间
                holder.textView_sendTime.setText(getSendTime(mList.get(position).getCreatedAt()));
                //设置发送文字
                if(mList.get(position).getContent()==null||mList.get(position).getContent().equals("")){
                    holder.textView_sendContent.setVisibility(View.GONE);
                }else {
                    holder.textView_sendContent.setVisibility(View.VISIBLE);
                    holder.textView_sendContent.setText((mList.get(position).getContent()));
                }
                //设置菜单按钮监听
                holder.imageView_selected.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mItemListener.onThreePointClick(view,position);
                    }
                });
                //设置点赞个数
                int num=mList.get(position).getPraiseNumber();
                if(num<999){
                    holder.textView_praiseNum.setText(num+"喜欢  ");
                }else {
                    holder.textView_praiseNum.setText("999+    ");
                }
                //设置评论个数
                holder.textView_commentNum.setText("  "+mList.get(position).getCommentNumber()+"评论");
                //设置爱心的样式
                holder.textView_heart.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mItemListener.onHeartClick(view,position);
                        holder.likeViewLayout.addLikeView();
                    }
                });
                if(mList.get(position).getIsPraise()==0){
                    holder.textView_heart.setTextColor(Color.parseColor("#b1c6d7"));
                    holder.textView_heart.setTextSize(15);
                    holder.textView_praiseNum.setTextColor(Color.parseColor("#828282"));
                }else {
                    holder.textView_heart.setTextColor(Color.parseColor("#FC81AF"));
                    holder.textView_heart.setTextSize(18);
                    holder.textView_praiseNum.setTextColor(Color.parseColor("#FC81AF"));
                }
                holder.itemView.setTag(position);    //标记item 用来检测哪个item被点击了

            }
        }else{
            return;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if(payloads.isEmpty()){
            onBindViewHolder(holder,position);
        }else{
            //设置点赞个数
            int num=mList.get(position).getPraiseNumber();
            if(num<999){
                holder.textView_praiseNum.setText(num+"喜欢  ");
            }else {
                holder.textView_praiseNum.setText("999+    ");
            }
            holder.textView_praiseNum.setTextColor(Color.parseColor("#FC81AF"));
            //Like 字体变化
            holder.textView_heart.setTextColor(Color.parseColor("#FC81AF"));
            holder.textView_heart.setTextSize(18);
        }
    }

    /** 重写这个方法，很重要，是加入Header和Footer的关键，我们通过判断item的类型，从而绑定不同的view    * */
    @Override
    public int getItemViewType(int position) {
        return TYPE_NORMAL;
    }

    @Override
    public int getItemCount() {

        //个数计算的是getListSize(mList) 即有几个动态有图片，应该失算有几个动态
        //NineGridTestModel  应该用table里面的那个类
        int size = getListSize(mList);
        return size;

    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        NineGridImageView<String> mNglContent;
        JustifyTextView textView_sendContent;
        TextView textView_sendTime;
        ImageView imageView_selected;       //三点
        TextView textView_heart;
        TextView textView_praiseNum;
        TextView textView_commentNum;

        FlowLikeView likeViewLayout;



        NineGridImageViewAdapter<String> mAdapter = new NineGridImageViewAdapter<String>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, String s) {
                displayImage(context, imageView, s, getPhotoImageOption());
            }

            @Override
            protected ImageView generateImageView(Context context) {
                return super.generateImageView(context);
            }

            @Override
            protected void onItemImageClick(Context context, ImageView imageView, int index, List<String> list) {
                ArrayList<String> arrayList = (ArrayList<String>) list;
                Intent intent=new Intent(context,FrescoLoaderPicturesActivity.class);
                intent.putStringArrayListExtra("photoUris",arrayList);
                intent.putExtra("currentPage",index);
                context.startActivity(intent);

            }

            @Override
            protected boolean onItemImageLongClick(Context context, ImageView imageView, int index, List<String> list) {
                //  Toast.makeText(context, "image long click position is " + index, Toast.LENGTH_SHORT).show();
                return true;
            }
        };



        public ViewHolder(View itemView) {

            super(itemView);
            mNglContent =  itemView.findViewById(R.id.ngl_images);
            mNglContent.setAdapter(mAdapter);

            textView_sendContent=itemView.findViewById(R.id.tv_sendcontent);
            textView_sendTime=itemView.findViewById(R.id.tv_sendtime);
            imageView_selected=itemView.findViewById(R.id.iv_selected);
            textView_heart= itemView.findViewById(R.id.tv_heart);
            textView_praiseNum=itemView.findViewById(R.id.tv_praisenum);
            textView_commentNum=itemView.findViewById(R.id.tv_commentnum);

            likeViewLayout =  itemView.findViewById(R.id.flowLikeView);

        }
    }
    private String getSendTime(String fullTime){
        String s=null;
        if(fullTime==null||fullTime.equals("")){
            fullTime="2017-12-31 15:22:14";
        }
        Calendar c = Calendar.getInstance();
        int currentYear = c.get(Calendar.YEAR);
        int currentmonth = c.get(Calendar.MONTH);
        int currentday = c.get(Calendar.DATE);
        Date fulldate;
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            fulldate = sdf.parse(fullTime);
            if(currentYear!=fulldate.getYear()+1900){
                //不是今年发的技能，显示全时间
                s=(fulldate.getYear()+1900)+"年"+(fulldate.getMonth()+1)+"月"+fulldate.getDate()+"日"+fulldate.getHours()+"时";
            }else if(currentmonth!=fulldate.getMonth()){
                //是今年 但不是这个月发的
                s=fulldate.getMonth()+"月"+fulldate.getDate()+"日"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else if(currentday-fulldate.getDate()==0){
                //是今年是这个月差一天
                s="今天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            } else if(currentday-fulldate.getDate()==1){
                //是今年是这个月差一天
                s="昨天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else if(currentday-fulldate.getDate()==2){
                //是今年是这个月差一天
                s="前天"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }else {
                s=(fulldate.getMonth()+1)+"月"+fulldate.getDate()+"日"+fulldate.getHours()+":"+((fulldate.getMinutes()<10)?("0"+fulldate.getMinutes()):fulldate.getMinutes());
            }
        } catch (ParseException ee) {
            ee.printStackTrace();
        }

        return s;
    }

    private int getListSize(List<AlumniCircle> list) {
        if (list == null || list.size() == 0) {
            return 0;
        }
        return list.size();
    }


}
